package module.edittroop
{
	import com.darwin.darwinIsometric.core.isoConst.IsoType;
	import com.darwin.darwinMVC.mvc.Command;
	import com.darwin.darwinUI.manager.UIManager;
	
	import main.AppConfig;
	import main.AppNet;
	import main.mvc.GController;
	
	import module.edittroop.cmds.EditTroopCmdConst;
	import module.edittroop.event.EditTroopEvent;
	import module.edittroop.model.EditTroopModelData;
	import module.edittroop.view.editnewtrooppanel.ChoosePanel;
	import module.edittroop.view.editnewtrooppanel.EditNewTroopPanel;
	import module.edittroop.view.troopinfopanel.EditTroopPanel;
	import module.edittroop.view.troopinfopanel.HeadFarem;
	import module.edittroop.view.troopinfopanel.HeroAvatarPanel;
	import module.scene.cmds.SceneCommand;
	import module.scene.consts.SceneConst;
	import module.scene.model.dataObj.TroopIsoDataObj;

	/**
	 *编组部队控制层 
	 * @author jichang
	 * 
	 */	
	public class EditTroopController extends GController
	{
		public function EditTroopController()
		{
			init();
		}
		/**
		 *编辑新部队面板 
		 */		
		private var _editNewTroop:EditNewTroopPanel;
		/**
		 *重组新部队面板 
		 */		
		private var _dissloveTroop:EditNewTroopPanel;
		/**
		 *部队信息面板 
		 */		
		private var _troopInfoPanel:EditTroopPanel;
		private var _modelData:EditTroopModelData;
		private function init():void{
			_headData =new Object;
			_modelData = new EditTroopModelData();
			_troopInfoPanel = new EditTroopPanel;
			_chose = new ChoosePanel;
			arr = new Array();
			_troopInfoPanel.addEventListener(EditTroopEvent.REMOVE_TROOP_EVENT,removeHandler);
			_troopInfoPanel._heroAvatarPanel.addEventListener(EditTroopEvent.GET_NEW_TROOP_PANEL,getNewEditTroopPanel);
			_troopInfoPanel._heroAvatarPanel.addEventListener(EditTroopEvent.LIST_ITEM_CLICK_EVENT,listClickHandle);
			_troopInfoPanel.addEventListener(EditTroopEvent.GET_DISSLOVE_TROOP_PANEL,dissloveHandel);
			_troopInfoPanel.addEventListener(EditTroopEvent.GET_CHANGLE_PANEL,getChanlePanel);
			_chose.addEventListener(EditTroopEvent.EDIT_DISSLOVE,dissloveTroop);
			registerAll();
		}
		private function getChanlePanel(e:EditTroopEvent):void
		{
			UIManager.getPopUpManager().addPopUp(_chose);
		}
		/**
		 *弹出重组队伍面板 
		 * @param e
		 * 
		 */		
		private var troopId:String;
		private function dissloveHandel(e:EditTroopEvent):void
		{
			_dissloveTroop = new EditNewTroopPanel();
			_dissloveTroop.addEventListener(EditTroopEvent.EDIT_DISSLOVE,dissloveTroop);
			_dissloveTroop.setData(_modelData.heroConfigArr,_modelData.armsConfigArr);
			if(_headData == null)
				return;
			var obj:Object = (_headData as HeadFarem).data;
			_dissloveTroop.txtNameInput.text = obj.troopName;
		 /*obj.attactNum;
			obj.defensNum;
			obj.armsName;
			obj.skill;
			obj.troops;*/
			_dissloveTroop.soldierInfoPanel.refleshData(obj);
			_dissloveTroop.titleLabel.text = "重组部队";
			UIManager.getPopUpManager().addPopUp(_dissloveTroop);
			_dissloveTroop.addEventListener(EditTroopEvent.REMOVE_NEW_TROOP_PANEL,removeHandler);
			_dissloveTroop.addEventListener(EditTroopEvent.EDIT_NEW_TROOP,editNewTroop);
		}
		private var _headData:Object;
		private function listClickHandle(e:EditTroopEvent):void
		{
			_headData = e.data;
			_troopInfoPanel.troopInfoPanel.changPanelInfo((e.data as HeadFarem).data);
			troopId= new String()
			troopId = ((e.data as HeadFarem).data).troopId
		}
		/**
		 *弹出编辑新部队对话框 
		 * @param e
		 * 
		 */		
		private function getNewEditTroopPanel(e:EditTroopEvent):void
		{
			var editNew:EditNewTroopPanel = new EditNewTroopPanel();
			_editNewTroop = editNew;
			_editNewTroop.setData(_modelData.heroConfigArr,_modelData.armsConfigArr);
			if(_headData.data)
			{
				_editNewTroop.txtNameInput.text = _headData.data.heroName;
			}
			else
			{
				_editNewTroop.txtNameInput.text = "小小正义";
			}
			_editNewTroop.titleLabel.text = "创建部队";
			UIManager.getPopUpManager().addPopUp(_editNewTroop);
			_editNewTroop.addEventListener(EditTroopEvent.REMOVE_NEW_TROOP_PANEL,removeHandler);
			_editNewTroop.addEventListener(EditTroopEvent.EDIT_NEW_TROOP,editNewTroop);
		}
		private function removeHandler(e:EditTroopEvent):void
		{
			switch(e.currentTarget)
			{
				case _troopInfoPanel:
					UIManager.getPopUpManager().removePopUp(_troopInfoPanel);
					break;
				case _editNewTroop:
					UIManager.getPopUpManager().removePopUp(_editNewTroop);
					break;
				case _dissloveTroop:
					UIManager.getPopUpManager().removePopUp(_dissloveTroop);
					break;
			}
		}
		private function registerAll():void
		{
			register(EditTroopCmdConst.EDIT_TROOP_INFO_PANEL,infoPanelFun);
			register(EditTroopCmdConst.EDIT_NEW_TROOP_SUCCESS,successHandle);
			register(EditTroopCmdConst.DELETE_TROOP_FROM_MODEL,deleteHandler);
		}
		private function deleteHandler(cmd:Command):void
		{
			(_headData as HeadFarem).dispose();
			_troopInfoPanel._heroAvatarPanel.list.source.splice(_headData.index,1);
			trace("返回删除成功");
		}
		private function successHandle(cmd:Command):void
		{
		/*	_troopInfoPanel._heroAvatarPanel.list.source.push(cmd.data);*/
		}
		/**
		 *处理弹出信息面板函数 
		 * @param cmd
		 * 
		 */		
		private var _eId:String;
		private function infoPanelFun(cmd:Command):void
		{
			_eId = cmd.data.eIde;
			UIManager.getPopUpManager().addPopUp(_troopInfoPanel);
		}
		/**
		 *处理弹出重组面板函数 
		 * @param cmd
		 * 
		 */		
		private function disslovePanelFun(cmd:Command):void
		{
			
		}
		/**
		 *处理弹出编辑新部队命令函数 
		 * @param cmd
		 * 
		 */		
		private function newTroopPanelFun(cmd:Command):void
		{
			_editNewTroop.txtNameInput.text = "小小正义";
		}
		/**
		 *接收到编辑部队信息  连接后台通知后台创建新部队 
		 * @param e
		 * 
		 */		
		private function editNewTroop(e:EditTroopEvent):void
		{	
			switch(e.currentTarget)
			{
				case _editNewTroop:
					var sendObj:Object = setObjTacket(e.data);
					sendToSocket(EditTroopCmdConst.EDIT_TROOP_METHO,sendObj);
					register(EditTroopCmdConst.EDIT_NEW_TROOP_SUCCESS,getGroupInfo);
					UIManager.getPopUpManager().removePopUp(_editNewTroop);
					UIManager.getPopUpManager().removePopUp(_troopInfoPanel);
					break;
				case _dissloveTroop:
					var $sendObj:Object = setObjTacket(e.data);
					sendToSocket(EditTroopCmdConst.EDIT_TROOP_METHO,$sendObj);
					UIManager.getPopUpManager().removePopUp(_dissloveTroop);
					break;
			}
		}
		private function setObjTacket(data:Object):Object
		{
			var obj:Object = new Object();
			var sendData:Object = new Object();
			obj.hero_id = data.heroId;
			obj.arms_id = data.armsId;
			obj.need_num = data.needNum;
			obj.troop_name = data.troop_name;
			obj.from_building = _eId;
			sendData.info = obj;
			return sendData;
		}
		/**
		 *发送数据到后台 
		 * 
		 */		
		private function sendToSocket(cmd:String,data:Object):void
		{
			AppNet.getInstance().send(cmd,data);
		}
		/**
		 *得到部队信息命令处理函数 
		 * @param cmd
		 * 
		 */		
		private var arr:Array;
		private function getGroupInfo(cmd:Command):void
		{
			var data:Object = new Object();
			data = cmd.data;
			var $cmd:SceneCommand = new SceneCommand(SceneCommand.SCENE_CMD_LOCATE_TROOP);
			$cmd.data = data;
			dispatch($cmd);
			arr.push(data);
			_troopInfoPanel._heroAvatarPanel.list.source = arr;
		}
		
		override public function start():void{
			/*UIManager.getPopUpManager().addEventListener(EditTroopEvent.EDIT_DISSLOVE,dissloveTroop);*/
		}
		/**
		 *玩家点击解散部队事件 
		 * @param eT
		 * 
		 */		
		private var _chose:ChoosePanel;
		public function dissloveTroop(e:EditTroopEvent):void
		{
			var $obj_1:Object = new Object();
			var $obj_2:Object = new Object();
			var $obj_3:Object = new Object();
			$obj_1.troop_id= troopId;
			$obj_2.troop_dissolve = $obj_1;
			$obj_3.info = $obj_2;
			sendToSocket(EditTroopCmdConst.RQ_DISSLOVE_TROOP_TO,$obj_3);
			//			sendToSocket();
			_modelData.disslove();
			//调用玩家部队信息删除该部队，抛出删除信息
		}
		private function dissloveTroopSuccess(cmd:Command):void
		{
			
		}
		public function get heroConfigArr():Array
		{
			return _modelData.heroConfigArr;
		}
	}
}